package com.demo.common.file;

import com.demo.common.vo.Result;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件处理controller
 */
@RestController
@RequestMapping("/files")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 上传文件
     * @param multipartFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile multipartFile){
        return Result.success(fileService.uploadFile(multipartFile));
    }

    /**
     * 获取文件
     * @param fileName
     * @param response
     */
    @SneakyThrows
    @GetMapping("/get")
    public void get(@RequestParam("fileName") String fileName, HttpServletResponse response){
        fileService.downLoadFile(fileName,response);
    }

    /**
     * 删除文件
     * @param fileName
     * @return
     */
    @SneakyThrows
    @PostMapping("/delete")
    public Result delete(@RequestParam("fileName") String fileName){
        fileService.deleteFile(fileName);
        return Result.success();
    }

}
